package top.yonyong.mynacos.configcenter.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.yonyong.mynacos.configcenter.container.MyConfigContainer;
import top.yonyong.mynacos.configcenter.dao.ConfigRepository;
import top.yonyong.mynacos.configcenter.pojo.ConfigInfo;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Describtion auto config
 * @Author yonyong
 * @Date 2020/7/6 9:59
 * @Version 1.0.0
 **/
@Configuration
@ConditionalOnClass(MyConfigContainer.class)
public class MyNacosAutoconfig {

    @Resource
    private ConfigRepository configRepository;

    /**
     *  db solution
     */
    @Bean
    @ConditionalOnMissingBean(MyConfigContainer.class)
    public MyConfigContainer myConfigContainer(){
        MyConfigContainer mc = MyConfigContainer.builder().build();

        final List<ConfigInfo> configInfos = configRepository.queryAllByGroupOrderByUpdateTime("DEFAULT");
        mc = mc.toBuilder().vals(configInfos).build();

        return mc;
    }
}
